package com.apache.passport.common;

/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache..passport.tools 
 */

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.apache.tools.StrUtil;

/**  
 * description:  白名单解析
 * @author Hou Dayu 创建时间：2014-9-22         
 */
public class XmlWhiteUtils {

	private static final String whiteFileName = "/white.xml";
	private long lastUpateTime = 0l;//最后修改时间
	private static XmlWhiteUtils instance;
	private String filePath = "";

	private XmlWhiteUtils() {
		filePath = getClassLoaderPath() + "config/";
	}

	public static XmlWhiteUtils getInstance() {
		if (null == instance) {
			instance = new XmlWhiteUtils();
		}
		return instance;
	}

	public boolean deWhiteXml(Map<String, ArrayList<String>> whiteMap) {
		ArrayList<String> whiteUrlArrList = new ArrayList<String>();
		ArrayList<String> whiteParadigmArrList = new ArrayList<String>();
		String whiteUrlStr = "whiteUrl";
		String whiteListStr = "whiteList";
		String whiteParadigmStr = "whiteParadigm";
		boolean flag = true;
		File oldfile = new File(filePath + whiteFileName);
		if (!oldfile.exists()) {
			whiteMap.put(whiteUrlStr, whiteUrlArrList);
			whiteMap.put(whiteParadigmStr, whiteParadigmArrList);
			flag = false;
			return flag;
		}
		long lastModletime = oldfile.lastModified();
		if (lastUpateTime < lastModletime) {//有更新从新加载
			try {
				SAXBuilder sb = new SAXBuilder();
				Document doc = null;
				doc = sb.build(oldfile);
				Element root = doc.getRootElement();
				Element whiteList = root.getChild(whiteListStr);

				Element whiteUrl = whiteList.getChild(whiteUrlStr);
				List<?> whiteListUrlList = whiteUrl.getChildren();
				//System.out.println("whiteListUrlList子节点个数:" + whiteListUrlList.size());
				for (int i = 0; i < whiteListUrlList.size(); i++) {
					Element em = (Element) whiteListUrlList.get(i);
					whiteUrlArrList.add(em.getValue());
				}
				whiteMap.put(whiteUrl.getName(), whiteUrlArrList);

				Element whiteParadigm = whiteList.getChild(whiteParadigmStr);
				List<?> whiteParadigmList = whiteParadigm.getChildren();
				//System.out.println("whiteParadigmList子节点个数:" + whiteParadigmList.size());
				for (int i = 0; i < whiteParadigmList.size(); i++) {
					Element em = (Element) whiteParadigmList.get(i);
					whiteParadigmArrList.add(em.getValue());
				}
				whiteMap.put(whiteParadigm.getName(), whiteParadigmArrList);

			} catch (JDOMException e) {
				flag = false;
				e.printStackTrace();
			} catch (IOException e) {
				flag = false;
				e.printStackTrace();
			}
		}
		return flag;
	}

	/**
	 * @return 返回classes的绝对路径。
	 */
	@SuppressWarnings("null")
	public String getClassLoaderPath() {
		String path = "";
		URL classLoaderUrl = Thread.currentThread().getContextClassLoader().getResource("");
		if (null == classLoaderUrl) {
			path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();//tomcat/weblogic
		} else {
			path = classLoaderUrl.getPath();
		}
		if (!StrUtil.isNull(path)) {
			if (path.startsWith(File.separator) || path.startsWith("/")) {
				String systemType = getSystemType();
				if ("windows".equals(systemType))
					path = path.substring(1);
			}
			if (!path.endsWith(File.separator) && !path.endsWith("/"))
				path += "/";
		}
		return path;
	}

	/**
	 * 获取操作系统类型
	 *
	 * @return 返回操作系统类型
	 */
	private String getSystemType() {
		String result = "windows";
		if (System.getProperty("os.name").equals("Linux")) {
			result = "linux";
		} else if ("Mac OS X".equals(System.getProperty("os.name"))) {
			result = "Mac OS X";
		}
		return result;
	}

	public static void main(String[] args) {
		Map<String, ArrayList<String>> whiteMap = new HashMap<String, ArrayList<String>>();
		while (true) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			XmlWhiteUtils.getInstance().deWhiteXml(whiteMap);
		}
	}

}
